<?php

	if (isset($_POST['edit-submit'])) {

		$result = $cn->Add('logs', array('userid'=>$user_id, 'date'=>date('Y-m-d H:i:s'), 'description'=>'User ['.$userdata['name'].'] edited ['.$table.'] result id ['.$cn->LastID().']', 'type'=>'edit'), false);

		$tbl = $table == 'companies' ? 'users' : $table;
		$result = $cn->Update($tbl, $_POST['id'], $_POST, true, array('edit-submit'));
		$return = isset($_SESSION['return_path']) ? $_SESSION['return_path'] : $path.$table;
		header('location: '.$return);
	
	} else {
		
		$labels		= array();
		$types		= array();
		$query = 'select * from '.$table.' where id='.$post_id;

		switch($table) {
			case 'subscription_types':
				$query = 'select id, label, freedays, dailyrate, price from subscription_types where id='.$post_id;
				$labels = array('id', 'Title', /*'Duration (days)',*/ 'Free Days', 'Daily Rate', 'Price');
				$types = array('readonly', '', /*'integer',*/ 'integer', 'decimal', 'decimal');
				break;

			case 'users':
				$query = 'select id,title,name,position,company,mob,email,address,province,linkedin,facebook,twitter,verified,accesscode from '.$table.' where id='.$post_id;
				$labels = array('id', 'Title', 'Name', 'Position', 'Company', 'Mobile', 'Email', 'Address', 'Province', 'LinkedIn URL', 'Facebook URL', 'Twitter URL', 'Verified', 'Access Code');
				$access = $admin == 1 ? '' : 'readonly';
				$types = array('readonly', '', '', '', '', 'telephone', '', '', 'dropdown|The Eastern Cape,The Free State,Gauteng,KwaZulu-Natal,Limpopo,Mpumalanga,The Northern Cape,North West,The Western Cape', '', '', '', 'checkbox', $access);

				break;

			case 'companies':
				$query = 'select id,company,name,mob,email,address,province,verified,accesscode from users where id='.$post_id;
				$types = array('readonly', '', '', 'telephone', '', '', 'dropdown|The Eastern Cape,The Free State,Gauteng,KwaZulu-Natal,Limpopo,Mpumalanga,The Northern Cape,North West,The Western Cape', 'checkbox', '');
				$labels = array('id', 'Company Name', 'Account Manager Name', 'Account Manager Mobile', 'Account Manager Email', 'Company Address', 'Company Province', 'Account Verified', 'Access Code');
				break;

			case 'buildings':
				$labels = array('id', 'Building Title', 'Manager', 'Telephone Number', 'Mobile Number', 'Email', 'Address', 'Province');
				$types = array('readonly', '', '', 'telephone', 'telephone', '', '', 'dropdown|The Eastern Cape,The Free State,Gauteng,KwaZulu-Natal,Limpopo,Mpumalanga,The Northern Cape,North West,The Western Cape');
				break;

			case 'services':
				$labels = array('id', 'Title', 'Description', 'Cost', 'Cost is per person');
				$types = array('readonly', '', '', 'decimal', 'checkbox');
				break;

			case 'training':
				$labels = array('id', 'Label', 'Description', 'Start Time', 'End Time', 'Minimum Capacity', 'Maximum Capacity', 'Price per delegate');
				$types = array('readonly', '', 'textarea', 'time', 'time', 'integer', 'integer', 'decimal');
				break;

			// Child Tables

			case 'subscriptions':
				$query = '';
				break;

			case 'company_users':
				$coid = $cn->Data('select companyid from company_users where id='.$post_id, false);
				$coid = $coid[0]['companyid'];
				$accesscode = $cn->Data('select accesscode from users where id='.$coid, false);
				if (sizeof($accesscode) > 0) $accesscode = $accesscode[0]['accesscode'];
				else $accesscode = '';
				$query = 'select id,name,mob,email,accesscode from '.$table.' where id='.$post_id;
				$labels = array('id', 'Name', 'Mobile Number', 'Email', 'Access Code');
				$types = array('readonly', '', 'telephone', '', 'readonly|'.$accesscode);
				break;

			case 'spaces':
				$query = 'select id,label,capacity,mintime,maxtime,hourlyrate from '.$table.' where id='.$post_id;
				$labels = array('id', 'Label', 'Capacity', 'Min Booking Time', 'Max Booking Time', 'Hourly Rate');
				$types = array('readonly', '', 'integer', 'decimal', 'decimal', 'decimal');
				break;

			default:
				$query = 'select * from '.$table.' where id='.$post_id;
		}

		echo $cn->EditForm($query, 'post', '', $labels, $types, false);
		//echo $query;
	}

?>